/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.dao.impl;

import java.util.List;

import com.eelpo.cmmp.core.hibernate.orm.QueryHints;
import com.eelpo.cmmp.core.spring.orm.jpa.BasicCommonDao;
import com.eelpo.cmmp.uac.dao.IActionManageDao;
import com.eelpo.cmmp.uac.dao.entity.TAction;
import com.eelpo.cmmp.uac.dao.vo.Action;
import com.eelpo.cmmp.uac.dao.vo.enumed.Status;
import com.eelpo.common.query.Expression;
import com.eelpo.common.query.JpaCriteria;
import com.eelpo.common.query.Order;
import com.eelpo.common.query.Result;
import com.eelpo.common.query.criteria.Criteria;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

/**
 * <pre>
 * 
 * 动作数据持久层具体实现
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Repository
public class ActionManageDao extends BasicCommonDao implements IActionManageDao {

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IActionManageDao#getActionById(java.lang.String)
	 */
	@Override
	public TAction getActionById(String id) throws DataAccessException {
		return find(TAction.class, id);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IActionManageDao#checkExist(java.lang.String)
	 */
	@Override
	public boolean checkExist(String code) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TAction.class);
		criteria.result(Result.count());
		criteria.add(Expression.eq("code", code));
		return getResultCount(criteria) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IActionManageDao#checkExistByUpdate(java.lang
	 *      .String, java.lang.String)
	 */
	@Override
	public boolean checkExistByUpdate(String id, String code) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TAction.class);
		criteria.result(Result.count());
		criteria.add(Expression.ne("id", id));
		criteria.add(Expression.eq("code", code));
		return getResultCount(criteria) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IActionManageDao#checkActionUsed(java.lang.String)
	 */
	@Override
	public boolean checkActionUsed(String id) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TAction.class, "action");
		criteria.result(Result.count());
		criteria.add(Expression.isNotEmpty("permissions"));
		criteria.add(Expression.eq("id", id));
		return getResultCount(criteria) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IActionManageDao#addAction(com.eelpo.cmmp.uac
	 *      .dao.entity.TAction)
	 */
	@Override
	public String addAction(TAction action) throws DataAccessException {
		persist(action);
		return action.getId();
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IActionManageDao#updateAction(com.eelpo.cmmp
	 *      .uac.dao.entity.TAction)
	 */
	@Override
	public void updateAction(TAction action) throws DataAccessException {
		merge(action);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IActionManageDao#deleteAction(java.lang.String)
	 */
	@Override
	public void deleteAction(String id) throws DataAccessException {
		remove(getReference(TAction.class, id));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IActionManageDao#getEnabledActions()
	 */
	@Override
	public List<TAction> getEnabledActions() throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TAction.class);
		criteria.add(Expression.eq("status", Status.ENABLED.getValue()));
		return getResultList(criteria);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IActionManageDao#getActionsPaing(com.eelpo.cmmp
	 *      .uac.dao.vo.Action, int, int)
	 */
	@Override
	public List<TAction> getActionsPaing(Action action, int first, int max) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TAction.class);
		criteria.add(Expression.ilike("code", action.getCode()));
		criteria.add(Expression.like("name", action.getName()));
		criteria.add(Expression.eq("status", Status.valueOf(action.getStatus())));
		criteria.order(Order.order(action.getSort()));
		criteria.setFirstResult(first);
		criteria.setMaxResults(max);
		criteria.setHint(QueryHints.HINT_CACHEABLE, true);
		return getResultList(criteria);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IActionManageDao#getActionsCount(com.eelpo.cmmp
	 *      .uac.dao.vo.Action)
	 */
	@Override
	public int getActionsCount(Action action) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TAction.class);
		criteria.result(Result.count());
		criteria.add(Expression.ilike("code", action.getCode()));
		criteria.add(Expression.like("name", action.getName()));
		criteria.add(Expression.eq("status", Status.valueOf(action.getStatus())));
		return getResultCount(criteria);
	}
}
